package com.itheima.health.controller.mobile;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.utils.SMSUtils;
import org.apache.xmlbeans.impl.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author deng
 * @package com.itheima.health.controller.mobile
 * @date Created in 2024/7/14 09:18
 */
@RestController("mobileValidatecode")
@RequestMapping("/validatecode")
public class ValidatecodeController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("send4Order.do")
    public Result send4Order(String telephone) {
        String s = stringRedisTemplate.opsForValue().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if (s==null){
            String code = SMSUtils.sendValidateCode(telephone);
            if (code!=null){
                stringRedisTemplate.opsForValue().set(telephone+RedisMessageConstant.SENDTYPE_ORDER,code,15, TimeUnit.MINUTES);
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            }
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return new Result(false, "验证码已存在");

    }

}
